/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-04<br/>
 * Creater: yangpengfei@think.cn <br/>
 */
var startPageNumber = 1;//起始页数
var totalPageNumber = 0;//总页数
var tableDataLength = 0;//总数据量
var currentPageNumber = 1;//当前页

layui.use(['element','table','layer','jquery','form'],function(){
	let element = layui.element;
	let table = layui.table;
	let layer = layui.layer;
	let $ = layui.jquery;
	let form = layui.form;
  	
  	
  	
	let tableOptions = {
 			 id: 'tableDictInfoItems',
 			 skin: 'line',
 			 elem: '#dictInfoTable',
 			 height: 'full-20',
 			 url: '/dict/list', //数据接口
 			 method:'get',
 			 where:{
 			   	dictType:$("input[name='dictType']").val()
 			 },
 			 even:true,
 			 page: {
 		    	groups:5,
 		    	curr:currentPageNumber,
 		    	limit:10,
 		    	limits:[10],
 		    	layout:['count','prev','page','next','limit','refresh','skip'],
 		    	jump:function(obj,first){
 		    		currentPageNumber = obj.curr;
 		    	}
 			 }, //开启分页
 			 loading:true,
 			 text:"当前暂无字典配置信息，请手动配置！",
 			 toolbar:"#captionToolbar",
 			 defaultToolbar:['filter', 'print', 'exports'],
 			 cols: [[ //表头
 			      {field: 'dictId', title: '编号',  sort: true, fixed: 'left',width:100,unresize:true}
 			      ,{field: 'dictType', title: '字典名称',unresize:true}
 			      ,{field: 'dictCode', title: '字典项编号',unresize:true}
 			      ,{field: 'dictDesc', title: '字典项内容',unresize:true}
 			     ,{field: 'createTime', title: '创建时间',unresize:true}
 			      ,{field: 'operate', title: '操作',toolbar: '#operateBarPanel',fixed: 'right',unresize:true}
 			    ]],
 			  done:function(res,curr,count){
 			    	tableDataLength = res.data.length;
 			  }
 		};
 	
 	const initTable = function (){
 		table.init('dictInfo',tableOptions);
 	}
 	initTable();
 	
 	const reloadTable = function (isEmpty) {
         table.reload("tableDictInfoItems",{
		    where:{
		    	dictType:$("input[name='dictType']").val()
		    },
		    page:{
				curr:isEmpty ?(currentPageNumber == 1 ? currentPageNumber : currentPageNumber-1):currentPageNumber
			}
         });
     };
     form.on('submit(dictInfoSearch)', function(data){
    	  reloadTable();
    	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    	});
     
     
   //头工具栏事件
 	  table.on('toolbar(dictInfo)', function(obj){
 	   
 	    switch(obj.event){
 	      case 'insert':
 	    	  layer.open({
 				  title:"新增字典信息"
 				  ,type: 1
 			        ,offset: 'auto' 
 			        ,id: 'addDictInfo' //防止重复弹出
 			        ,content: $('#addDictInfoContainer')
 			        ,btn: ['确认', '取消']
 			        ,btnAlign: 'c' //按钮居中
 			        ,closeBtn:2
 			        ,shade: 0.5 //不显示遮罩
 			        ,shadeClose:true
 			        ,anim:1
 			        ,resize:false
 			        ,success: function(layero, index){
 			        	$("#dictIdVal").val("");
 			        	$("#dictTypeVal").val("");
 			        	$("#dictCodeVal").val("");
 			        	$("#dictDescVal").val("");
 			        }
 			        ,yes: function(){
 			        //向服务端发送删除指令
 				      $.ajax({
 				    	  url:"/dict/insert",
 				    	  type:"PUT",
 				    	  data:{
 				    		  "dictType":$("#dictTypeVal").val(),
 				    		  "dictCode":$("#dictCodeVal").val(),
 				    		  "dictDesc":$("#dictDescVal").val()
 				    	  },
 				    	  dataType:"json",
 				    	  success:function(result){
 				    		  if(typeof(result) != "undefined" && result != null && result != ""){
 				    			  if(result.code == 0){
 				    				 layer.closeAll();
 				    				  reloadTable();
 				    			      layer.msg(result.msg, {icon: 6}); 
 				    			  }else{
 				    				  layer.msg(result.msg, {icon: 5});
 				    			  }
 				    		  }
 				    	  },error:function(){
 				    		  layer.msg("新增字典项请求失败", {icon: 5});
 				    	  }
 				      });
 			        },
 			        cancel:function(index, layero){
 			        	$("#dictIdVal").val("");
 			        	$("#dictTypeVal").val("");
 			        	$("#dictCodeVal").val("");
 			        	$("#dictDescVal").val("");
 			        	layer.close(index);
 			        }
 			  });
 	      break;
 	      
 	    };
 	  });
 	  
 	  
 	  //监听操作栏按钮事件
 	  table.on('tool(dictInfo)',function(obj){
 		 
 		  var nowData = obj.data;
 		  var layEvent = obj.event;
 		  //修改信息
 		  if(layEvent === 'edit'){
 			 layer.open({
				  title:"更新字典信息"
				  ,type: 1
			        ,offset: 'auto' 
			        ,id: 'updateDictInfo' //防止重复弹出
			        ,content: $('#addDictInfoContainer')
			        ,btn: ['确认', '取消']
			        ,btnAlign: 'c' //按钮居中
			        ,closeBtn:2
			        ,shade: 0.5 //不显示遮罩
			        ,shadeClose:true
			        ,anim:1
			        ,resize:false
			        ,success: function(layero, index){
			        	$("#dictIdVal").val(nowData.dictId);
			        	$("#dictTypeVal").val(nowData.dictType);
			        	$("#dictCodeVal").val(nowData.dictCode);
			        	$("#dictDescVal").val(nowData.dictDesc);
			        }
			        ,yes: function(){
			        //向服务端发送删除指令
				      $.ajax({
				    	  url:"/dict/update",
				    	  type:"POST",
				    	  data:{
				    		  "dictId":$("#dictIdVal").val(),
				    		  "dictType":$("#dictTypeVal").val(),
				    		  "dictCode":$("#dictCodeVal").val(),
				    		  "dictDesc":$("#dictDescVal").val()
				    	  },
				    	  dataType:"json",
				    	  success:function(result){
				    		  if(typeof(result) != "undefined" && result != null && result != ""){
				    			  if(result.code == 0){
				    				 layer.closeAll();
				    				  reloadTable();
				    			      layer.msg(result.msg, {icon: 6}); 
				    			  }else{
				    				  layer.msg(result.msg, {icon: 5});
				    			  }
				    		  }
				    	  },error:function(){
				    		  layer.msg("更新字典项请求失败", {icon: 5});
				    	  }
				      });
			        },
			        cancel:function(index, layero){
			        	$("#dictIdVal").val("");
			        	$("#dictTypeVal").val("");
			        	$("#dictCodeVal").val("");
			        	$("#dictDescVal").val("");
			        	layer.close(index);
			        }
			  });
 		  //删除信息
 		  }else if (layEvent === 'del'){
 			  layer.confirm('真的删除字典项['+ nowData.dictType +'-'+ nowData.dictCode +']吗？',{icon: 3, title:'提示'}, function(index){
 			      
 			      //向服务端发送删除指令
 			      $.ajax({
 			    	  url:"/dict/del/"+nowData.dictId,
 			    	  type:"DELETE",
 			    	  dataType:"json",
 			    	  success:function(result){
 			    		  if(typeof(result) != "undefined" && result != null && result != ""){
 			    			  layer.close(index);
 			    			  if(result.code == 0){
 			    				 reloadTable();
 			    			      layer.msg(result.msg, {icon: 6}); 
 			    			  }else{
 			    				  layer.msg(result.msg, {icon: 5});
 			    			  }
 			    		  }
 			    	  },error:function(){
 			    		  layer.msg("删除字典项处理失败", {icon: 5});
 			    	  }
 			      });
 			  });
 			  
 			  
 		  }
 	  });
});